﻿// Gateway.Api 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using System.Linq.Expressions;
using SqlSugar;

namespace Aurora.AI.SqlSugar.Framework.Repositories;

/// <summary>
/// 仓储接口
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IRepository<T> where T : class, new()
{
    #region 通用方法
    /// <summary>
    /// db
    /// </summary>
    /// <returns></returns>
    SqlSugarScope GetDb();

    /// <summary>
    /// 获取所有list
    /// </summary>
    /// <returns></returns>
    List<T> GetList();

    /// <summary>
    /// 获取所有list-异步
    /// </summary>
    Task<List<T>> GetListAsync();

    /// <summary>
    /// 根据lambda查询
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    List<T> GetList(Expression<Func<T, bool>> whereExpression);

    /// <summary>
    /// 根据lambda查询-异步
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression);

    /// <summary>
    /// 获取分页列表
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="page"></param>
    /// <param name="size"></param>
    /// <param name="orderExpression"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    Task<(int total, List<T> list)> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int page, int size,
        Expression<Func<T, object>>? orderExpression = null, OrderByType type = OrderByType.Asc);

    /// <summary>
    /// 根据lambda表达式获取数量
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    int Count(Expression<Func<T, bool>> whereExpression);

    /// <summary>
    /// 根据lambda表达式获取数量-异步
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    Task<int> CountAsync(Expression<Func<T, bool>> whereExpression);


    /// <summary>
    /// 根据id获取实体
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    T GetById(dynamic id);

    /// <summary>
    /// 根据id获取实体-异步
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    Task<T> GetByIdAsync(dynamic id);
    /// <summary>
    /// 根据lambda获取单个对象 （注意，需要确保唯一，如果获取到2个会报错，这种场景需要使用GetFirst）
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    T GetSingle(Expression<Func<T, bool>> whereExpression);

    /// <summary>
    /// 根据lambda获取单个对象-异步  （注意，需要确保唯一，如果获取到2个会报错，这种场景需要使用GetFirst）
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression);

    /// <summary>
    /// 根据lambda获取单个对象
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    T GetFirst(Expression<Func<T, bool>> whereExpression);

    /// <summary>
    /// 根据lambda获取单个对象 --异步
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    Task<T?> GetFirstAsync(Expression<Func<T, bool>> whereExpression);

    /// <summary>
    /// 实体插入
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    bool Insert(T obj);

    /// <summary>
    /// 实体插入-异步
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    Task<bool> InsertAsync(T obj);

    /// <summary>
    /// 批量插入
    /// </summary>
    /// <param name="objs"></param>
    /// <returns></returns>
    bool InsertRange(List<T> objs);

    /// <summary>
    /// 批量插入-异步
    /// </summary>
    /// <param name="objs"></param>
    /// <returns></returns>
    Task<bool> InsertRangeAsync(List<T> objs);

    /// <summary>
    /// 插入返回自增列
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    int InsertReturnIdentity(T obj);

    /// <summary>
    /// 插入返回自增列-异步
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    Task<int> InsertReturnIdentityAsync(T obj);

    /// <summary>
    /// 插入返回long id
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    long InsertReturnBigIdentity(T obj);

    /// <summary>
    /// 插入返回long id-异步
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    Task<long> InsertReturnBigIdentityAsync(T obj);

    /// <summary>
    /// 批量删除
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    bool DeleteByIds(dynamic[] ids);

    /// <summary>
    /// 批量删除-异步
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    Task<bool> DeleteByIdsAsync(dynamic[] ids);

    /// <summary>
    /// 根据主键删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    bool Delete(dynamic id);

    /// <summary>
    /// 根据主键删除-异步
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    Task<bool> DeleteAsync(dynamic id);

    /// <summary>
    /// 根据实体删除
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>

    bool Delete(T obj);

    /// <summary>
    /// 根据实体删除-异步
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    Task<bool> DeleteAsync(T obj);

    /// <summary>
    /// 根据表达式删除
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    bool Delete(Expression<Func<T, bool>> whereExpression);

    /// <summary>
    /// 根据表达式删除-异步
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression);

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    bool Update(T obj);

    /// <summary>
    /// 更新-异步
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    Task<bool> UpdateAsync(T obj);

    /// <summary>
    /// 批量更新
    /// </summary>
    /// <param name="objs"></param>
    /// <returns></returns>
    bool UpdateRange(List<T> objs);


    /// <summary>
    /// 批量更新-异步
    /// </summary>
    /// <param name="objs"></param>
    /// <returns></returns>
    Task<bool> UpdateRangeAsync(List<T> objs);


    /// <summary>
    /// 是否包含元素
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    bool IsAny(Expression<Func<T, bool>> whereExpression);


    /// <summary>
    /// 是否包含元素-异步
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression);


    #endregion 通用方法
}
